<?php 
class MW_Simplenews_Adminhtml_CategoryController extends Mage_Adminhtml_Controller_Action
{
	public  function indexAction()
	{
		$this->loadLayout()->renderLayout();
	}

	public function newAction(){
		$this->_forward('edit');
	}

	public function editAction(){
		$id     = $this->getRequest()->getParam('id');
		$model  = Mage::getModel('simplenews/category')->load($id);
		if ($model->getId() || $id == 0) {
			$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
			if (!empty($data)) {
				$model->setData($data);
			}

			Mage::register('simplenews_data', $model);

			$this->loadLayout();
			$this->_setActiveMenu('simplenews/simplenews');

			$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Category Manager'), Mage::helper('adminhtml')->__('Category Manager'));
			// $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));

			$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

			$this->_addContent($this->getLayout()->createBlock('simplenews/adminhtml_category_edit'))
				->_addLeft($this->getLayout()->createBlock('simplenews/adminhtml_category_edit_tabs'));

			$this->renderLayout();

		} else {
			Mage::getSingleton('adminhtml/session')->addError(Mage::helper('simplenews')->__('Item does not exist'));
			$this->_redirect('*/*/');
		}
	}

	public function saveAction() {
		if ($data = $this->getRequest()->getPost()) {

			$model = Mage::getModel('simplenews/category');		
			$model->setData($data)->setId($this->getRequest()->getParam('id'));
			
			try {
				$model->save();
				Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('simplenews')->__('Item was successfully saved'));
				Mage::getSingleton('adminhtml/session')->setFormData(false);

				if ($this->getRequest()->getParam('back')) {
					$this->_redirect('*/*/edit', array('id' => $model->getId()));
					return;
				}
				$this->_redirect('*/*/');
				return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setFormData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('example')->__('Unable to find item to save'));
        $this->_redirect('*/*/');
	}


	public function deleteAction(){
		if( $this->getRequest()->getParam('id') > 0 ) {
			try {
				$model = Mage::getModel('simplenews/category');
				 
				$model->setId($this->getRequest()->getParam('id'))
					->delete();
					 
				Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
				$this->_redirect('*/*/');
			} catch (Exception $e) {
				Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
				$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
			}
		}
		$this->_redirect('*/*/');
	}
}